package com.stone.inventory.sharding.infra.algorithm;

import com.stone.inventory.sharding.config.properties.DbShardingProperty;
import com.stone.inventory.sharding.config.properties.TableRuleProperty;
import com.stone.inventory.sharding.infra.hash.KetamaHash;

/**
 * 事务数据分片算法
 *
 * @author Mr_wenpan@163.com 2021/8/16 11:22 下午
 */
public abstract class AbstractShardingByPosCodeAlgorithm extends AbstractShardingAlgorithm<String> {

    private KetamaHash ketamaHash;

    public AbstractShardingByPosCodeAlgorithm(DbShardingProperty dbShardingProperty) {
        super(dbShardingProperty);
        TableRuleProperty tableRuleProperty = getTableRuleProperty();
        if (null != tableRuleProperty) {
            ketamaHash = new KetamaHash(tableRuleProperty.getActualTables());
        }
    }

    @Override
    public String getTargetTableByShardingValue(String shardingValue) {
        return ketamaHash.getActualNode(shardingValue);
    }
}